/**
 * Created by chenyu on 17/10/17.
 */
var Promise = require('bluebird');
var area = require('mongoose').model('Area');

module.exports = {
    delete: function (id) {
        var defer = Promise.defer();
        area.findOne({region_name: id}).exec(function (err, data) {
            if (err) {
                console.log(err);
                return defer.reject('delete station error')
            }
            if (!data) {
                return defer.resolve({status: false, message: 'not exist'})
            } else {
                data.remove(function (err) {
                    defer.resolve({status: true})
                })
            }
        });
        return defer.promise
    },
    update: function (query, info) {
        var defer = Promise.defer();
        delete info._id;
        area.findOne(query).exec(function (err, data) {
            if (err) {
                console.log(err);
                return defer.reject('delete station error')
            }
            if (!data) {
                return defer.resolve({status: false, message: 'not exist'})
            } else {
                area.update(query, info).exec(function (err, data) {
                    if (err) {
                        console.log(err);
                        return defer.reject('update error')
                    } else {
                        defer.resolve({status: true, message: data})
                    }
                });
            }
        });
        return defer.promise
    },
    all: function () {
        var defer = Promise.defer();
        area.where().exec(function (err, data) {
            if (err) {
                defer.reject('find all error')
            } else {
                defer.resolve(data)
            }
        });
        return defer.promise;
    },
    create: function (data) {
        var defer = Promise.defer();
        area.create(data, function (err, data) {
            if (err) {
                return defer.reject('创建失败')
            }
            defer.resolve({status: true, message: data})
        });
        return defer.promise;
    }
};